﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class PlayerCardController : MonoBehaviour
{
    public int ownPlayerLocationIndex;
    public List<int> list = new List<int>();

    public bool  AddPitchOnCardToList(int num)
    {
        if (list.Count < 2)
        {
            list.Add(num);
            return true;  
        }
        else if(list.Count == 2)
        {
            CheckSendDealCardMsg(num);
        }
        return false;
    }

    public bool RemovePitchOnCardFromList(int num)
    {
        if (list.Contains(num))
        {
            list.Remove(num);
            return true;
        }
        return false;
    }

    private void CheckSendDealCardMsg(int num)
    {
        string temp =   GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].cardType;

        Debug.Log("玩家本地保存的牌型" + temp);

        if (temp == "五小" || temp == "五花" || temp == "炸弹")
        {
            MessageManager.GetInstance().SendMsg1210(GameData.GetInstance().locationIndex, 1, GetDealCards(num) );
        }
        else
        {
            CheckNiuAndSendMsg(num);
        }
    }

    private int[] GetDealCards(int num)
    {
        List<int> tempList = new List<int>();
        tempList.Add(list[0]);
        Debug.Log("添加第一张牌" + list[0]);
        tempList.Add(list[1]);
        Debug.Log("添加第二张牌" + list[1]);
        tempList.Add(num);
        Debug.Log("添加第三张牌" + num);

        for (int i = 0; i < GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].handCardsList.Count; i++)
        {
            if (!tempList.Contains(GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].handCardsList[i]))
            {
                tempList.Add(GameRoomModel.GetInstance().playerDic[GameData.GetInstance().locationIndex].handCardsList[i]);
            }
        }

        int[] cards = new int[5];
        for (int i = 0; i < 5; i++)
        {
            cards[i] = tempList[i];
        }

        return cards;
    }

    private  void CheckNiuAndSendMsg(int num)
    {
        int NiuValue3 = GetNiuValue(num);
        int NiuValue1 = GetNiuValue(list[0]);
        int NiuValue2 = GetNiuValue(list[1]);

        if ((NiuValue1 + NiuValue2 +NiuValue3)%10 == 0 )
        {
            MessageManager.GetInstance().SendMsg1210(GameData.GetInstance().locationIndex, 1, GetDealCards(num));
        }
    }

    private int GetNiuValue(int num)
    {
        int temp = 0;
        if (num < 37)
        {
            temp = (num - 1)/4 +1;
        }
        else
        {
            temp = 10;
        }
        return temp;
    }
}
